package isr.cloudgate.dashboard.servlet;

import isr.google.gdata.GDataClient;
import isr.google.gdata.data.GoogleGroupMember;

import java.io.IOException;
import java.util.Collection;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class GroupMemberServlet
 */
public class GroupOwnerServlet extends HttpServlet {

	private static final Logger logger = Logger
		.getLogger(GroupOwnerServlet.class.getName());
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		logger.info("group owner request (post)");

		GDataClient client = null;

		try {
			String groupId = request.getParameter("group");
			if (groupId == null) {
				logger.warning("no group parameter found "
					+ "in group owner request");
				return;
			}

			client = ServletUtilities.getAppsClient(request);
			if (client == null) {
				logger.warning("no apps client");
				return;
			}

			Collection<GoogleGroupMember> owners =
				client.getGroupOwners(groupId);
			request.setAttribute("groupId", groupId);
			request.setAttribute("owners", owners);

		} catch (Exception e) {

			logger.warning(e.toString());
			request.setAttribute("userMessage", e.toString());

		}

		request.getRequestDispatcher(JspPage.group_owners).forward(request,
			response);

	}

}
